//
//  ViewController.m
//  DragChangeNavBar
//
//  Created by QQ920924960 on 16/9/27.
//  Copyright © 2016年 zmosa. All rights reserved.
//

#import "ViewController.h"
static NSString *cellID = @"cellID";
#define kHeaderH (fk_width * 0.618)

@interface ViewController ()<UIGestureRecognizerDelegate, UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UIView *navBar;
@property (nonatomic, strong) UIImageView *scaleImageView;
@property (nonatomic, weak) UITableView *tableView;
@property (nonatomic, assign) CGFloat lastOffsetY;
@end

@implementation ViewController

- (UIView *)navBar
{
    if (!_navBar) {
        _navBar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, fk_width, 64)];
        _navBar.backgroundColor = [UIColor orangeColor];
    }
    return _navBar;
}

- (UIImageView *)scaleImageView
{
    if (!_scaleImageView) {
        _scaleImageView = [[UIImageView alloc] init];
        _scaleImageView.contentMode = UIViewContentModeScaleToFill;
        _scaleImageView.clipsToBounds = true;
        _scaleImageView.image = [UIImage imageNamed:@"123"];
        _scaleImageView.backgroundColor = [UIColor blueColor];
        
        CGFloat scaleImageViewX = 0;
        CGFloat scaleImageViewY = 0;
        CGFloat scaleImageViewH = self.scaleImageView.image.size.height;
        _scaleImageView.frame = CGRectMake(scaleImageViewX, scaleImageViewY, fk_width, scaleImageViewH);
    }
    return _scaleImageView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupSubviews];
}

- (void)viewWillAppear:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:true];
    [super viewWillAppear:animated];
}

- (void)setupSubviews
{
    self.title = @"头部下拉";
    self.lastOffsetY = -kHeaderH;
    
    // 创建tableview
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView = tableView;
    [self.view addSubview:tableView];
    tableView.dataSource = self;
    tableView.delegate = self;
    // 下移tableview的内容
    tableView.contentInset = UIEdgeInsetsMake(kHeaderH, 0, 0, 0);
    
    [self.view addSubview:self.scaleImageView];
    [self.view addSubview:self.navBar];
    
    // 移除navbar自动64高度
    self.automaticallyAdjustsScrollViewInsets = false;
    // 修复侧滑失效
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 50;
}

#pragma mark - UITableViewDelegate
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    cell.imageView.backgroundColor = [UIColor redColor];
    cell.textLabel.text = [NSString stringWithFormat:@"---(%zd)---", indexPath.row];
    return cell;
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    // 计算当前偏移位置
    CGFloat offsetY = scrollView.contentOffset.y;
    CGFloat delta = offsetY - self.lastOffsetY;
    CGFloat height = kHeaderH - delta;
    if (height < fk_navBarH) {
        height = fk_navBarH;
    }
    CGRect tempF = self.scaleImageView.frame;
    tempF.size.height = height;
    self.scaleImageView.frame = tempF;
    
    CGFloat alpha = delta / (kHeaderH - fk_navBarH);
    if (alpha >= 1) {
        alpha = 0.99;
    }
    self.navBar.alpha = alpha;
}
@end
